<?php


namespace App\Models;


use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

/**
 * Class Order
 * @package App\Models
 * @property Express $express
 * @property Customer $customer
 * @property Collection $products
 * @property OrderShipment $shipment
 * @property OrderInvtRefund $invtRefund
 * @property OrderPack $pack
 * @property OrderCustoms $customs
 */
class Order extends Model
{
    protected $fillable = [
        'number', 'customer_id', 'customer_enterprises_id', 'express_id', 'freight', 'buyer_name', 'receiving_name', 'receiving_phone', 'buyer_type', 'buyer_number', 'buyer_telephone', 'areas', 'address', 'remark', 'price', 'code', 'original_number', 'weight', 'ebc_code', 'ebc_name', 'ebp_code', 'ebp_name', 'net_weight',
    ];

    /**
     * @param array $value
     * @author luffyzhao@vip.126.com
     */
    protected function setAreasAttribute(array $value)
    {
        $this->attributes['areas'] = implode('|', $value);
    }

    /**
     * @return array
     * @author luffyzhao@vip.126.com
     */
    protected function getAreasAttribute()
    {
        return explode('|', $this->attributes['areas']);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     * @author luffyzhao@vip.126.com
     */
    public function products()
    {
        return $this->hasMany(OrderProduct::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     * @author luffyzhao@vip.126.com
     */
    public function logs()
    {
        return $this->hasMany(OrderLog::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function customer()
    {
        return $this->belongsTo(Customer::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function express()
    {
        return $this->belongsTo(Express::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function customs()
    {
        return $this->hasOne(OrderCustoms::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function invtRefund(){
        return $this->hasOne(OrderInvtRefund::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function shipment(){
        return $this->hasOne(OrderShipment::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     * @author luffyzhao@vip.126.com
     */
    public function config(){
        return $this->hasMany(OrderConfig::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     * @author luffyzhao@vip.126.com
     */
    public function pack(){
        return $this->hasOne(OrderPack::class);
    }
}
